<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="账号名 " prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号名 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行卡号" prop="bank">
        <el-input
          v-model="queryParams.bank"
          placeholder="请输入银行卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户行" prop="openBank">
        <el-input
          v-model="queryParams.openBank"
          placeholder="请输入开户行"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:skzh:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:skzh:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:skzh:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:skzh:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:skzh:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-radio-group
      v-model="queryParams.reviewStatus"
      style="margin-bottom: 10px"
      size="mini"
      @change="listTab()"
    >
      <el-radio-button label="">收款账号管理</el-radio-button>
      <el-radio-button label="1">收款账号审核</el-radio-button>
    </el-radio-group>
    <el-table
      v-loading="loading"
      :data="skzhList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="账号名 " align="center" prop="account" />
      <el-table-column label="银行卡号" align="center" prop="bank" />
      <el-table-column label="开户行" align="center" prop="openBank" />
      <el-table-column label="开户地址" align="center" prop="openAddress" />
      <el-table-column label="审核状态" align="center" prop="reviewStatus">
        <template slot-scope="scope">
          {{ scope.row.reviewStatus == "0" ? "已通过" : "审核中" }}
        </template>
      </el-table-column>
      <el-table-column label="审批类型" align="center" prop="reviewType" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleChangeReviewStatus(scope.row.skzhId)"
            v-if="scope.row.reviewStatus == '1'"
            v-hasPermi="['system:skzh:editstatus']"
            >通过</el-button
          >
          <el-button
            v-if="scope.row.withdrawalStatus == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:skzh:edit']"
            >修改</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.withdrawalStatus == '1'"
            v-hasPermi="['system:skzh:remove']"
            >删除</el-button
          >

          <el-button v-else size="mini" type="text">提现中</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号名 " prop="account">
          <el-input v-model="form.account" placeholder="请输入账号名 " />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bank">
          <el-input v-model="form.bank" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="开户行" prop="openBank">
          <el-input v-model="form.openBank" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="开户地址" prop="openAddress">
          <el-input v-model="form.openAddress" placeholder="请输入开户地址" />
        </el-form-item>

        <el-form-item
          label="部门/团队"
          prop="deptId"
          v-if="$store.state.user.roleId != 3 && $store.state.user.roleId != 4"
        >
          <treeselect
            :disable-branch-nodes="true"
            v-model="form.deptId"
            :options="enabledDeptOptions"
            :show-count="true"
            placeholder="请选择归属部门/团队"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSkzh,
  getSkzh,
  delSkzh,
  addSkzh,
  updateSkzh,
  changeReviewStatus,
  deptTreeSelect
} from "@/api/system/skzh";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Skzh",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      skzhList: [],
      enabledDeptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: null,
        bank: null,
        openBank: null,
        openAddress: null,
        reviewStatus: "",
        reviewType: null,
        withdrawalStatus: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        account: [
          { required: true, message: "账号名不能为空", trigger: "blur" },
        ],
        bank: [
          { required: true, message: "银行卡号不能为空", trigger: "blur" },
        ],
        openBank: [
          { required: true, message: "开户行不能为空", trigger: "blur" },
        ],
        openAddress: [
          { required: true, message: "开户地址不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "入驻商不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    getDeptTree() {
      deptTreeSelect().then((response) => {
        const deptList = this.filterDisabledDept(
          JSON.parse(JSON.stringify(response.data))
        );
        this.enabledDeptOptions = deptList.filter((v) => v.id != 1);
      });
    },
    // 过滤禁用的部门/团队
    filterDisabledDept(deptList) {
      return deptList.filter((dept) => {
        if (dept.disabled) {
          return false;
        }
        if (dept.children && dept.children.length) {
          dept.children = this.filterDisabledDept(dept.children);
        }
        return true;
      });
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listSkzh(this.queryParams).then((response) => {
        this.skzhList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        skzhId: null,
        account: null,
        bank: null,
        openBank: null,
        openAddress: null,
        reviewStatus: null,
        reviewType: null,
        withdrawalStatus: null,
        deptId: null,
        updateTime: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    listTab() {
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.skzhId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const skzhId = row.skzhId || this.ids;
      getSkzh(skzhId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },

    handleChangeReviewStatus(skzhId) {
      this.$modal
        .confirm('是否通过编号为"' + skzhId + '"的数据项？')
        .then(function () {
          return changeReviewStatus({ skzhId });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("审核通过");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.skzhId != null) {
            updateSkzh(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSkzh(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const skzhIds = row.skzhId || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + skzhIds + '"的数据项？')
        .then(function () {
          return delSkzh(skzhIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/skzh/export",
        {
          ...this.queryParams,
        },
        `skzh_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
